package cn.yusei.teamTribe.service.team;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.yusei.teamTribe.constant.PermissionType;
import cn.yusei.teamTribe.constant.ResponseCode;
import cn.yusei.teamTribe.constant.RoleType;
import cn.yusei.teamTribe.dao.team.TeamRoleDAO;
import cn.yusei.teamTribe.dao.team.po.TeamRolePO;
import cn.yusei.teamTribe.dao.user.UserDAO;
import cn.yusei.teamTribe.service.PermissionService;

@Service
@Transactional
public class TeamRoleService {

	@Autowired
	private TeamRoleDAO teamRoleDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private TeamMemberService tmService;
	@Value("${maxTeamManager}")
	private int maxTeamManager;
	
	/**
	 * 
	 * @param requestUserId 发出请求的用户 id
	 * @param teamId
	 * @param roleId
	 * @param userId
	 * @return
	 */
	public int add(int requestUserId, int teamId, int roleId, int userId)
	{
		if(userDAO.selectById(userId) == null)
			return ResponseCode.USER_NOT_EXISTS;
		//对象不在 team 中
		if(!tmService.isMember(userId, teamId)) {
			return ResponseCode.NOT_IN_TEAM;
		}
		switch (roleId)
		{
			case RoleType.TEAM_MASTER:
				return resetTeamMaster(requestUserId, teamId, userId);
			case RoleType.TEAM_MANAGER:
				return addTeamManager(requestUserId, teamId, userId);
		}
		return ResponseCode.SERVER_ERROR;
	}
	
	private int addTeamManager(int requestUserId, int teamId, int userId)
	{
		if(!permissionService.havePermission(requestUserId, teamId, PermissionType.SET_TEAM_MANAGER,0))
			return ResponseCode.PERMISSION_FORBIDDEN;
		//不能重复设置管理员，会长不能同时是管理员
		if(teamRoleDAO.exists(teamId, new int[] {RoleType.TEAM_MASTER, RoleType.TEAM_MANAGER}, userId))
			return ResponseCode.ROLE_CONFLICT;
		TeamRolePO teamRolePO = new TeamRolePO();
		teamRolePO.setTeamId(teamId);
		teamRolePO.setRoleId(RoleType.TEAM_MANAGER);
		teamRolePO.setUserId(userId);
		if(teamRoleDAO.cntOf(teamRolePO) >= maxTeamManager)
			return ResponseCode.ROLE_NUMBER_LIMIT;
		teamRoleDAO.insert(teamRolePO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	private int resetTeamMaster(int requestUserId, int teamId, int userId)
	{
		if(!permissionService.havePermission(requestUserId, teamId, PermissionType.SET_TEAM_MASTER,0))
			return ResponseCode.PERMISSION_FORBIDDEN;
		TeamRolePO teamRolePO  = new TeamRolePO();
		teamRolePO.setTeamId(teamId);
		teamRolePO.setRoleId(RoleType.TEAM_MASTER);
		teamRolePO.setUserId(userId);
		//userId 本来就是会长
		if(teamRoleDAO.exists(teamId, new int[] {RoleType.TEAM_MASTER},userId))
			return ResponseCode.REQUEST_SUCCESSED;
		 //不是管理员或会长，更新会长为 userId
		if(!teamRoleDAO.exists(teamId, new int[] {RoleType.TEAM_MANAGER},userId))
		{
			teamRoleDAO.updateUserId(teamRolePO);
			return ResponseCode.REQUEST_SUCCESSED;
		}
		//已经是管理员，删除原会长，修改 userId 的 roleId 为会长
		teamRoleDAO.deleteRole(teamRolePO);
		teamRoleDAO.updateRoleId(teamRolePO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public int[] get(int teamId, int roleId)
	{
		TeamRolePO teamRolePO = new TeamRolePO();
		teamRolePO.setTeamId(teamId);
		teamRolePO.setRoleId(roleId);
		int[] ids = teamRoleDAO.selectUser(teamRolePO);
		return ids;
	}
	
	public int delete(int requestUserId, int teamId, int roleId, int userId)
	{
		switch (roleId)
		{
			case RoleType.TEAM_MANAGER:
				return deleteManager(requestUserId, teamId, userId);
		}
		return ResponseCode.PARAM_ILLEGAL;
	}
	
	public int deleteManager(int requestUserId, int teamId, int userId)
	{
		if(!permissionService.havePermission(requestUserId, teamId, PermissionType.SET_TEAM_MANAGER,0))
			return ResponseCode.PERMISSION_FORBIDDEN;
		TeamRolePO teamRolePO = new TeamRolePO();
		teamRolePO.setTeamId(teamId);
		teamRolePO.setUserId(userId);
		teamRoleDAO.deleteRole(teamRolePO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
}
